package com.financing.controller.app;
import java.util.HashMap;
import java.util.Map;

import com.financing.model.invest.Trade;
import com.financing.model.system.Logger;
import com.financing.model.user.User;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Money;
import com.financing.util.Str;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;

public class AdminController extends Controller {
	/**
	 * 后台页面控制器
	 * @throws Exception 
	 */
	@Clear 
	@Before({SessionInViewInterceptor.class})
	public void page() throws Exception {
		String page = getPara(0);  
		String page2 = getPara(1); 
		setAttr("para",getParaMap()); 
		if(Str.nb(page2)){
			render(page+"/"+page2+".html");
		}else{
			render(page+".html");
		}
	} 	
	
	/**
	 * 后台数据统计
	 * @throws Exception 
	 */
	public void statistics() throws Exception {
		
		Map<String,Long> map = new HashMap<String,Long>();
		String today = DateUtil.getNowDate()+" 00:00:00";
		
		// 今日注册人数
		User u = User.me.findFirst("select count(*) as count from user where create_time>=?",today);
		map.put("reg_today", Money.getLong(u.getLong("count")));
		
		// 总注册人数
		u = User.me.findFirst("select count(*) as count from user");
		map.put("reg_total", Money.getLong(u.getLong("count")));
		
		// 今日实名人数
		u = User.me.findFirst("select count(*) as count from user where create_time>=? and is_bind_bank_card=1",today);
		map.put("realname_today", Money.getLong(u.getLong("count")));
		
		// 总实名人数
		u = User.me.findFirst("select count(*) as count from user where is_bind_bank_card=1");
		map.put("realname_all", Money.getLong(u.getLong("count")));
		
		// 今日投资人次
		Trade t = Trade.me.findFirst("select count(*) as count from trade where create_time>=?",today);
		map.put("trade_user_today", Money.getLong(t.getLong("count")));
		 
		// 总投资人次
		t = Trade.me.findFirst("select count(*) as count from trade");
		map.put("trade_user_all", Money.getLong(t.getLong("count")));
		
		// 今日投资金额
		t = Trade.me.findFirst("select sum(amount) as total from trade where create_time>=?",today);
		map.put("trade_amount_today", Money.getLong(t.getLong("total")));
		
		// 总投资金额
		t = Trade.me.findFirst("select sum(amount) as total from trade");
		map.put("trade_amount_all", Money.getLong(t.getLong("total")));
		
		// 今日访问人数
		Logger l = Logger.me.findFirst("select count(*) as count from logger where (name='登陆' or name='退出') and type<>'backend' and create_time>=?",today);
		map.put("login_today", Money.getLong(l.getLong("count")));
		
		// 总访问人数
		l = Logger.me.findFirst("select count(*) as count from logger where (name='登陆' or name='退出') and type<>'backend' ");
		map.put("login_all", Money.getLong(l.getLong("count")));
		
		renderJson(Json.ok("数据查询成功！",map));
	} 
}
 